home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Sprocket Framework DR2 / Sprocket Starter / SprocketStarter Code / SprocketStarter.cp < prev    next >
Text File  |  1996-06-15  |  11KB  |  477 lines

  1. /*
  2.  
  3.     File:        SprocketStarter.cp
  4.     Project:    Sample code for Sprocket Framework 1.1 (DR2), released 6/15/96
  5.     Contains:    Boilerplate application-specific code
  6.     To Do:        Whatever your heart desires
  7.  
  8.     Sprocket Major Contributors:
  9.     ----------------------------
  10.     Dave Falkenburg, producer of Sprocket 1.0
  11.     Bill Hayden,     producer of Sprocket 1.1
  12.     Steve Sisak,     producer of the upcoming Sprocket 2.0
  13.     
  14.     Pete Alexander        Steve Falkenburg    Randy Thelen
  15.     Eric Berdahl        Nitin Ganatra        Chris K. Thomas
  16.     Marshall Clow        Dave Hershey        Leonard Rosenthal
  17.     Tim Craycroft        Dave Mark            Dean Yu
  18.     David denBoer        Gary Powell
  19.     Cameron Esfahani    Jon Summers            Apple Computer, Inc.
  20.         
  21.     Comments, Additions, or Corrections:
  22.     ------------------------------------
  23.     Bill Hayden, Nikol Software <nikol@codewell.com>
  24.  
  25. */
  26.  
  27.  
  28.  
  29. #include "SprocketStarter.h"
  30.  
  31. #include "Sprocket.h"
  32. #include "UDialog.h"
  33. #include "Window.h"
  34. #include "StandardMenus.h"
  35. #include "SplashWindow.h"
  36. #include "FuturesDemo.h"
  37.  
  38. #include "SerialNumberProtection.h"
  39. #include "SpokenCommandHandler.h"
  40.  
  41. #include <Menus.h>
  42. #include <Devices.h>
  43.  
  44. #include "TextWindow.h"
  45. #include "SampleWindow.h"
  46.  
  47. #include "PreferencesDialogWindow.h"
  48. #include "MPPreferencesDialogWindow.h"
  49.  
  50.  
  51. TSerialNumberProtection*    SN;
  52.  
  53. #if GENERATINGPOWERPC
  54. TSpokenCommandHandler*        SCH;
  55. #endif
  56.  
  57. //    Function Prototypes:
  58.  
  59. void    AboutBox(void);
  60. void    OpenExistingDocument(void);
  61. OSErr    CreateNewTextWindow(void);
  62. OSErr    CreateNewSampleWindow(void);
  63.  
  64. Boolean    CheckRegistration(void);
  65.  
  66. #define    kAboutBoxFor68K                    256
  67. #define    kAboutBoxForPowerPC                257
  68.  
  69.  
  70. ////////////////////////////////////////////////////////////////////
  71. //
  72. //    SetupApplication
  73. //
  74. //    Modify this function to provide any application-specific
  75. //    initialization you may require.
  76. //
  77. //    This is a good place to call EnterMovies, allocate
  78. //    sound channels, setup network things, etc.
  79. //
  80. //  For kicks, I am sercuring this application with Serial Number protection
  81.  
  82. OSErr SetupApplication(void)
  83. {
  84.     if (!CheckRegistration())
  85.         ;    // a normal app would quit here due to incorrect registration, but this is just a sample
  86.  
  87. #if GENERATINGPOWERPC
  88.     if (gHasSpeechRecognitionManager)
  89.         {
  90.         gSplashWindow->NewStatus("\pLoading Speech Recognition Handler…");
  91.         
  92.         SCH = new TSpokenCommandHandler;
  93.         if (SCH)
  94.             {
  95.             OSErr    err;
  96.             
  97.             err = SCH->ISpokenCommandHandler();
  98.             if (noErr == err)
  99.                 {
  100.                 // Register our spoken commands.  Could this be any easier?
  101.                 
  102.                 SCH->RegisterSpokenCommand("Cut\0", cCut);
  103.                 SCH->RegisterSpokenCommand("Copy\0", cCopy);
  104.                 SCH->RegisterSpokenCommand("Paste\0", cPaste);
  105.                 SCH->RegisterSpokenCommand("Clear\0", cClear);
  106.                 SCH->RegisterSpokenCommand("Undo\0", cUndo);
  107.                 SCH->RegisterSpokenCommand("Select All\0", cSelectAll);
  108.                 SCH->RegisterSpokenCommand("Close\0", cClose);
  109.                 
  110.                 SCH->RegisterSpokenCommand("Black\0", cBlack);
  111.                 SCH->RegisterSpokenCommand("Blue\0", cBlue);
  112.                 SCH->RegisterSpokenCommand("Green\0", cGreen);
  113.                 SCH->RegisterSpokenCommand("Red\0", cRed);
  114.                 SCH->RegisterSpokenCommand("Pink\0", cPink);
  115.                 SCH->RegisterSpokenCommand("Orange\0", cOrange);
  116.                 SCH->RegisterSpokenCommand("Brown\0", cBrown);
  117.  
  118.                 SCH->RegisterSpokenCommand("Plain\0", cPlainText);
  119.                 SCH->RegisterSpokenCommand("Bold\0", cBold);
  120.                 SCH->RegisterSpokenCommand("Italic\0", cItalic);
  121.                 SCH->RegisterSpokenCommand("Underline\0", cUnderline);
  122.  
  123.                 err = SCH->RegisterSpokenCommand("Save\0", cSave);
  124.                 
  125.                 if (!err)
  126.                     err = SCH->StartListening();
  127.                     
  128.                 if (err)
  129.                     ErrorReporter(err, __FILE__, __LINE__);
  130.  
  131.                 gSplashWindow->NewStatus("\pSpeech Recognition Handler Loaded");
  132.                 }
  133.             else
  134.                 {
  135.                 ErrorReporter(err, __FILE__, __LINE__);
  136.                 delete SCH;
  137.                 }
  138.             }
  139.         }
  140. #endif
  141.  
  142.     InitCursor();
  143.     
  144.     gSplashWindow->NewStatus("\pLoading Futures Demo…");
  145.     InitFuturesDemo();
  146.     
  147.     return noErr;
  148. }
  149.  
  150.  
  151. ////////////////////////////////////////////////////////////////////
  152. //
  153. //    CheckRegistration
  154. //
  155. //    Check to see if the app is registered - this function is not required for Sprocket
  156.  
  157. Boolean    CheckRegistration(void)
  158. {
  159.     OSErr    err;
  160.             
  161.     SN = new TSerialNumberProtection(32000);
  162.     
  163.     if (SN)
  164.         {
  165.         Str32    Serial;
  166.         Str255    Name, Company;
  167.         
  168.         gSplashWindow->NewStatus("\pChecking Registration…");
  169.         err = SN->ReadRegistration(Serial, Name, Company);
  170.         if (err || Name[0] == 0)
  171.             {
  172.             short itemHit = 0;
  173.             DialogRef    aDialog = GetNewDialog(259, nil, (WindowRef)-1);
  174.             
  175.             SetDialogDefaultItem(aDialog, 1);
  176.             SetDialogCancelItem(aDialog, 2);
  177.             SetDialogTracksCursor(aDialog, true);
  178.             
  179.             while (itemHit != 1 && itemHit != 2)
  180.                 {
  181.                 ModalDialog(StandardDialogFilterUPP, &itemHit);
  182.                 }
  183.             
  184.             GetEditText(aDialog, 3, Name);
  185.             GetEditText(aDialog, 4, Company);
  186.             GetEditText(aDialog, 5, Serial);
  187.             
  188.             HideWindow(GetDialogWindow(aDialog));
  189.             DisposeDialog(aDialog);
  190.             
  191.             err = SN->WriteRegistration(Serial, Name, Company);
  192.             }
  193.         
  194.         if (err == noErr)
  195.             {
  196.             gSplashWindow->NewStatus("\pRegistering on the network…");
  197.             err = SN->EnableProtection();
  198.             if (err)
  199.                 {
  200.                 err = SN->ReadRegistration(Serial, Name, Company);
  201.                 ParamText("\pThe serial number ", Serial, "\p is already in use, or an AppleTalk error occured.", "\p");
  202.                 StandardAlert(128);
  203.                 }
  204.             }
  205.         }
  206.         
  207.     if (!SN || err)
  208.         {
  209.         Str255    errStr;
  210.         NumToString(err, errStr);
  211.         ParamText("\pI was unable to verify the serial number of this application due to error ", errStr, "\p.", "\p");
  212.         StandardAlert(128);
  213.         return false;
  214.         }
  215.         
  216.     return true;
  217. }
  218.  
  219. ////////////////////////////////////////////////////////////////////
  220. //
  221. //    CreateNewTextWindow
  222. //
  223. //    Create a new Text window
  224.  
  225. OSErr CreateNewTextWindow(void)
  226. {
  227.     TTextWindow    *aNewWindow = new TTextWindow();
  228.     
  229.     if (aNewWindow)
  230.         return aNewWindow->ITextWindow();
  231.     else
  232.         return memFullErr;
  233. }
  234.  
  235.  
  236. ////////////////////////////////////////////////////////////////////
  237. //
  238. //    CreateNewSampleWindow
  239. //
  240. //    Create a new Sample window
  241.  
  242. OSErr CreateNewSampleWindow(void)
  243. {
  244.     TSampleWindow    *aNewWindow = new TSampleWindow();
  245.     
  246.     if (!aNewWindow)
  247.         return memFullErr;
  248.         
  249.     return noErr;
  250. }
  251.  
  252.  
  253. ////////////////////////////////////////////////////////////////////
  254. //
  255. //    TearDownApplication
  256. //
  257. //    Modify this function to tear down anything that you allocated
  258. //    from within SetupApplication.
  259.  
  260. void TearDownApplication(void)
  261. {
  262.     CleanupFuturesDemo();
  263.  
  264.     if (SN)
  265.         delete SN;
  266.  
  267. #if GENERATINGPOWERPC
  268.     if (SCH)
  269.         delete SCH;
  270. #endif
  271. }
  272.  
  273.  
  274. ////////////////////////////////////////////////////////////////////
  275. //
  276. //    HandleMenuCommand
  277. //
  278. //    Modify this function to handle any registered menu commands
  279. //    that aren’t specific to a particular window.
  280.  
  281. void HandleCommand(CommandID theCommand)
  282. {
  283.     OSErr    err;
  284.     
  285.     switch (theCommand)
  286.         {
  287.         case    cAbout:
  288.             AboutBox();
  289.             break;
  290.             
  291.         case    cNewTextWindow:
  292.             CreateNewTextWindow();
  293.             break;
  294.             
  295.         case    cNewSampleWindow:
  296.             CreateNewSampleWindow();
  297.             break;
  298.             
  299.         case    cOpen:
  300.             OpenExistingDocument();
  301.             break;
  302.             
  303.         case    cPreferences:            
  304.             err = gMenuBar->HiliteMenusForModalDialog(false);
  305.             if (err)
  306.                 ErrorReporter(err, __FILE__, __LINE__);
  307.  
  308.             TPreferencesDialogWindow * prefsDialog = new TPreferencesDialogWindow;
  309.             break;
  310.             
  311.         case    cMPPreferences:
  312.             TMPPreferencesDialogWindow * MPprefsDialog = new TMPPreferencesDialogWindow;
  313.             
  314.             err = gMenuBar->HiliteMenusForModalDialog(false);
  315.             if (err)
  316.                 ErrorReporter(err, __FILE__, __LINE__);
  317.  
  318.             if (MPprefsDialog)
  319.                 MPprefsDialog->IMultiPanelDialogWindow(1, 2, 3);
  320.             break;
  321.                     
  322.         case    cPing:
  323.             SendSimpleAEvt(kSillyEventClass, kPing1Event);
  324.             break;
  325.  
  326.         case    cPing2:
  327.             SendSimpleAEvt(kSillyEventClass, kPing2Event);
  328.             break;
  329.  
  330.         default:
  331.             break;
  332.         }
  333. }
  334.  
  335.  
  336. ////////////////////////////////////////////////////////////////////
  337. //
  338. //    HandleMenuSelection
  339. //
  340. //    Modify this function to handle any menu selections that aren’t
  341. //    specific to a particular window. Normally, you shouldn’t need to
  342. //    do anything, unless you hate using MenuCommands.
  343.  
  344. void HandleMenuSelection(MenuID /* theMenu */, MenuItemID /* theItem */)
  345. {
  346. }
  347.  
  348.  
  349. ////////////////////////////////////////////////////////////////////
  350. //
  351. //    ReadLocalClipboardFromScrap & WriteLocalClipboardToScrap
  352. //
  353. //    These functions will be called whenever the user switches into
  354. //    or out of your application.
  355. //
  356. //    If you keep copy of the clipboard in your own data structures,
  357. //    these functions will allow you to keep it in synch so that the
  358. //    user can cut and paste information between your program and
  359. //    other applications.
  360. //
  361. //    We don’t actually use the clipboard in SprocketSample, so these
  362. //    functions are empty for now.
  363.  
  364. void ReadLocalClipboardFromScrap(void)
  365. {
  366. }
  367.  
  368. void WriteLocalClipboardToScrap(void)
  369. {
  370. }
  371.  
  372.  
  373. ////////////////////////////////////////////////////////////////////
  374. //
  375. //    CreateNewDocument, OpenDocument, PrintDocument, and
  376. //    QuitApplication.
  377. //
  378. //    These functions will be called whenever one of the required
  379. //    AppleEvents is sent to your application— either when your
  380. //    program is launched, or when the user opens a document from
  381. //    the Finder.
  382.  
  383. OSErr CreateNewDocument(void)
  384. {
  385.     return CreateNewTextWindow();
  386. }
  387.  
  388.  
  389. OSErr OpenDocument(FSSpec* theDocument, void * /*unused*/)
  390. {
  391.     TTextWindow    *aNewWindow = new TTextWindow();
  392.     
  393.     if (aNewWindow)
  394.         return aNewWindow->ITextWindow(theDocument);
  395.     else
  396.         return memFullErr;
  397. }
  398.  
  399.  
  400. OSErr PrintDocument(FSSpec* /* theDocument */, void * /*unused*/)
  401. {
  402.     //    We don’t do printing yet, but you could.
  403.     return errAEEventNotHandled;
  404. }
  405.  
  406.  
  407. Boolean QuitApplication(void)
  408. {
  409.     //    Just go ahead and say we’re done quitting…
  410.     //    We have no app-specific clean-up or chores to do here, but you might.
  411.     
  412.     return true;
  413. }
  414.  
  415.  
  416.  
  417. ////////////////////////////////////////////////////////////////////
  418. //
  419. //    AboutBox
  420. //
  421. //    Put up our about box, including the version number. Depending
  422. //    on what version we are running, pick the correct DLOG resource.
  423. //
  424.  
  425. void AboutBox(void)
  426. {
  427.     Handle            versionHandle;
  428.     StringPtr        shortVersionString;
  429.     short            itemHit;
  430.     Str32            Serial;
  431.     Str255            Name, Company;
  432.  
  433.     SN->ReadRegistration(Serial, Name, Company);
  434.  
  435.     versionHandle = GetResource('vers', 1);
  436.     if (versionHandle)
  437.         {
  438.         shortVersionString = (StringPtr) ((char *) *versionHandle + 6);
  439.         ParamText(shortVersionString, Name, Company, Serial);
  440.         ReleaseResource(versionHandle);
  441.         }
  442.  
  443. #if GENERATING68K
  444.     itemHit = StandardAlert(kAboutBoxFor68K);
  445. #else
  446.     itemHit = StandardAlert(kAboutBoxForPowerPC);
  447. #endif
  448. }
  449.  
  450.  
  451.  
  452. ////////////////////////////////////////////////////////////////////
  453. //
  454. //    OpenExistingDocument
  455. //
  456. //    Use StandardFile to ask the user for a file to open. We use
  457. //    CustomGetFile so that we can properly handle update events
  458. //    in other windows while the dialog is active.
  459.  
  460. void OpenExistingDocument(void)
  461. {
  462.     StandardFileReply    reply;
  463.     SFTypeList            ourTypes;
  464.     
  465.     Point    where = { -1, -1 };
  466.     
  467.     HiliteWindowsForModalDialog(false);
  468.     CustomGetFile((FileFilterYDUPP) nil, -1, ourTypes, &reply, 0, where,
  469.                   (DlgHookYDUPP) nil, StandardDialogFilterYDUPP, nil, nil, nil);
  470.     HiliteWindowsForModalDialog(true);
  471.     
  472.     if (reply.sfGood)
  473.         {
  474.         OpenDocument(&reply.sfFile, nil);
  475.         }
  476. }
  477.